package cn.stylefeng.guns.sys.modular.tao.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.base.shiro.annotion.Permission;
import cn.stylefeng.guns.sys.core.constant.Const;
import cn.stylefeng.guns.sys.modular.system.warpper.MenuWrapper;
import cn.stylefeng.guns.sys.modular.tao.entity.Classification;
import cn.stylefeng.guns.sys.modular.tao.entity.Goods;
import cn.stylefeng.guns.sys.modular.tao.model.params.ClassificationParam;
import cn.stylefeng.guns.sys.modular.tao.service.ClassificationService;
import cn.stylefeng.guns.sys.modular.tao.service.GoodsService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


/**
 * 商品分类分类管理表
 */

@Controller
@RequestMapping("/classification")
public class ClassificationController extends BaseController {

    private String PREFIX = "/classification";

    @Autowired
    private ClassificationService classificationService;

    @Autowired
    private GoodsService goodsService;
    /**
     * 跳转到主页面
     *
     * @author
     * @Date 2019-09-02
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/classification.html";
    }

    /**
     * 新增页面
     *
     * @author
     * @Date 2019-09-02
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/classification_add.html";
    }

    /**
     * 编辑页面
     *
     * @author
     * @Date 2019-09-02
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/classification_edit.html";
    }

    /**
     * 新增接口
     *
     * @author
     * @Date 2019-09-02
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(ClassificationParam classificationParam) {
        Classification detail = this.classificationService.getByName(classificationParam.getName());
        if(classificationParam.getParentCode()!=null&&classificationParam.getName()!=null&&!"".equals(classificationParam.getName())){
            if (detail == null) {
                //添加到商品分类表中
                Integer adds = classificationService.adds(classificationParam);
                //再次查询该品牌 此时才有该分类id
                Classification details = this.classificationService.getByName(classificationParam.getName());
                // 根据选择分类   修改商品表中的品牌分类
                Integer add =    goodsService.updateByClass(details);
                return ResponseData.success();
            } else {
                return ResponseData.error(500, "请勿重复添加！");
            }
        }else{
            return ResponseData.error(500, "请正确填选！");
        }


    }

    /**
     * 根据名称查找分类
     */
    @RequestMapping("/getParentName")
    @ResponseBody
    public ResponseData getParentName(HttpServletResponse response) {
        List<Classification> Classification = classificationService.getParentName();
        return ResponseData.success(Classification);
    }

    /**
     * 获取所有商品品牌分类  二级分类为erp中的商品的品牌分类
     */
    @RequestMapping("/getGoodsClass")
    @ResponseBody
    public ResponseData getLevel2And(HttpServletResponse response) {
        List<Goods> Classification = goodsService.getGoodsClass();
        return ResponseData.success(Classification);
    }

    /**
     * 编辑接口
     *
     * @author
     * @Date 2019-09-02
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(ClassificationParam classificationParam) {
        this.classificationService.update(classificationParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author
     * @Date 2019-09-02
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(ClassificationParam classificationParam) {
        List<Integer> ids = classificationService.getChildById(classificationParam.getId());
        if (ids.size() > 0) {
            for (Integer id : ids) {
                classificationService.deleteById(id);
            }
            this.classificationService.delete(classificationParam);
            return ResponseData.success();
        } else {
            this.classificationService.delete(classificationParam);
            return ResponseData.success();
        }
    }

    /**
     * 查看详情接口
     *
     * @author
     * @Date 2019-09-02
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(ClassificationParam classificationParam) {
        Classification detail = this.classificationService.getById(classificationParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author
     * @Date 2019-09-02
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(ClassificationParam classificationParam) {
        return this.classificationService.findPageBySpec(classificationParam);
    }


    /**
     * 获取菜单列表（s树形）
     *
     * @author fengshuonan
     * @Date 2019年2月23日22:01:47
     */

    @RequestMapping(value = "/listTree")
    @ResponseBody
    public Object listTree(@RequestParam(required = false) String menuName,
                           @RequestParam(required = false) String level) {
        List<Map<String, Object>> menus = classificationService.selectMenuTree(menuName, level);
        List<Map<String, Object>> menusWrap = new MenuWrapper(menus).wrap();
        LayuiPageInfo result = new LayuiPageInfo();
        result.setData(menusWrap);
        return result;
    }

    /**
     * 查询分类
     *
     * @author lcw
     * @Date 2019-11-18
     */
    @ResponseBody
    @RequestMapping("/getMenu")
    public ResponseData getMenu(ClassificationParam classificationParam) {
        List<Classification> list = classificationService.getMenu();
        return ResponseData.success(list);
    }

    /**
     * 查询分类商品
     *
     * @author lcw
     * @Date 2019-11-18
     */
    @ResponseBody
    @RequestMapping("/getGoodsById")
    public ResponseData getGoodsById(ClassificationParam classificationParam) {
        List<Classification> list = classificationService.getGoodsById(classificationParam.getId().toString());
        return ResponseData.success(list);
    }

}


